<!DOCTYPE html>
<html
        xmlns:th="http://www.thymeleaf.org"
        xmlns:sec="http://www.thymeleaf.org/extras/spring-security5"
        lang="zh">
<head>
    <meta charset="utf-8">
    <title>列表模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../../../layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../../../layuiadmin/style/admin.css"
          media="all">
    <script th:src="@{/js/hope.jquery.form.js}"></script>

</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label for="userName" class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="userName" name="userName" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label for="roleName" class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input type="text" id="roleName" name="roleName" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label for="dictGqflValue" class="layui-form-label">股权分类</label>
                    <div class="layui-input-block">
                        <select id="dictGqflValue" name="dictGqflValue">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label for="dictIdKhfl" class="layui-form-label">考核分类</label>
                    <div class="layui-input-inline">
                        <select id="dictIdKhfl" name="dictIdKhfl">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="lay-table-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;" class="layui-hide">
                <button class="layui-btn layuiadmin-btn-useradmin layui-hide" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin layui-hide" data-type="add">添加</button>
            </div>

            <table id="lay-table" lay-filter="lay-table"></table>
        </div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>


<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;


        //表格实例
        table.render({
            elem: '#lay-table'
            , url: 'layGrid' //数据接口
            , page: true //开启分页
            , limit: 10
            , toolbar: 'default'
            , cols: [
                [
                    {type: 'numbers', title: '序号', fixed: 'left'},
                    {field: 'id', title: '主键', width: 200, hide: true},
                    {field: 'loginName', title: '登录名称'},
                    {field: 'userName', title: '单位名称'},
                    {
                        title: '考核类型', templet: function (d) {
                            var khfl = "";
                            $.each(d.hasKhfl, function (i, o) {
                                khfl += o.dictName + ";";
                            })
                            return khfl;
                        }
                    },
                    {
                        title: '角色', templet: function (d) {
                            var roleName = "";
                            $.each(d.hasRoles, function (i, o) {
                                roleName += o.roleName + ";";
                            })
                            return roleName;
                        }
                    },
                    {
                        field: 'deleted', title: '是否删除',hide:true, templet: function (d) {
                            return d === true ? '是' : '否';
                        }
                    },
                    {
                        field: 'createTime ', title: '添加日期', templet: function (d) {
                            return layui.util.toDateString(d.createTime, "yyyy-MM-dd");
                        }
                    },
                    {title: "操作", templet: "#table_op", width: 280}
                ]]
        });

        //监听搜索
        form.on('submit(lay-table-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('lay-table', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('lay-table')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('lay-table');
                        layer.msg('已删除');
                        layer.close(index);
                    });
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , title: '添加用户'
                    , content: 'add.html'
                    , maxmin: true
                    , area: ['80%', '80%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        submitJqueryForm($, index, layero, function (result) {
                            if (result.code === 0) {
                                layer.msg('保存成功');
                                table.reload('lay-table');
                            } else {
                                layer.alert('保存失败');
                            }
                        });
                        //mhf实现方法
                        // var iframeWindow = window['layui-layer-iframe' + index]
                        //     //,submitID = 'LAY-user-front-submit'
                        //     , submitID = 'lay-submit'
                        //     , submit = layero.find('iframe').contents().find('#' + submitID);
                        //
                        // //监听提交
                        // iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        //     let formData = new FormData(iframeWindow.document.getElementById("lay-form"));
                        //     var field = data.field; //获取提交的字段
                        //     //console.log(field);
                        //
                        //     //提交 Ajax 成功后，静态更新表格中的数据
                        //     $.ajax({
                        //         url: '/system/user/add',
                        //         type: 'post',
                        //         //data: JSON.stringify(data.field),
                        //         //contentType: 'application/json',
                        //         data: formData,
                        //         processData: false,
                        //         contentType: false,
                        //         cache: false,
                        //         async: true,
                        //         success: function (data) {
                        //             layer.msg('添加成功');
                        //             table.reload('lay-table'); //数据刷新
                        //         },
                        //         error: function (data) {
                        //             layer.alert('添加失败，原因：' + JSON.stringify(data));
                        //         }
                        //     });
                        //     //table.reload('lay-table'); //数据刷新
                        //     layer.close(index); //关闭弹层
                        // });
                        //
                        // submit.trigger('click');
                    }
                });
                //layer.full(indexAdd);
            }
        };


        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听模板按钮
        table.on('tool(lay-table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            switch (layEvent) {
                case 'edit':
                    var indexEdit = layer.open({
                        type: 2,
                        //content: 'edit/' + obj.data.userId,
                        content: 'edit/' + data.id,
                        maxmin: true,
                        area: ['80%', '80%'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            submitJqueryForm($, index, layero, function (result) {
                                if (result.code === 0) {
                                    layer.close(indexEdit);
                                    layer.msg('保存成功');
                                    table.reload('lay-table')
                                } else {
                                    layer.alert('保存失败');
                                }
                                //console.log(result);
                            });
                        },
                    });
                    break;
                case 'delete':
                    //console.log(obj.param);

                    layer.confirm('确定删除此用户？', {icon: 3, title: '提示'}, function (index) {
                        $.post("/system/user/delete/" + data.id, function (result) {
                            if (result.code === 0) {
                                layer.msg('删除成功');
                                tr.remove();
                                //table.reload('lay-table');
                            } else {
                                layer.alert('删除失败');
                            }
                        });
                    });
                    break;
                case 'managerCharge':
                    var indexCharge = layer.open({
                        type: 2,
                        title: '设置领导主管部门',
                        content: 'manager_charge?chargeUserId=' + data.id,
                        maxmin: true,
                        area: ['80%', '80%'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            submitJqueryForm(layui.$, index, layero, function (result) {
                                console.log(result)
                                if (result.code === 0) {
                                    layer.close(indexCharge);
                                    layer.msg('设置成功');
                                } else {
                                    layer.msg('设置失败');
                                }
                            });
                        }
                    });
                    break;
                case 'view':
                    var indexEdit = layer.open({
                        type: 2,
                        content: 'edit/' + data.id,
                        maxmin: true,
                        area: ['80%', '80%'],
                        yes: function (index, layero) {
                            submitJqueryForm($, index, layero, function (result) {
                                if (result.code === 0) {
                                    layer.close(indexEdit);
                                    layer.msg('保存成功');
                                    table.reload('lay-table')
                                } else {
                                    layer.alert('保存失败');
                                }
                                //console.log(result);
                            });
                        },
                    });
                    break;
                default:
                    break;
            }
            //console.log(obj.data.userId)
        });

        //监听表格头部工具栏
        table.on('toolbar(lay-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    var indexAdd = layer.open({
                        type: 2,
                        content: ['add'],
                        maxmin: true,
                        area: ['80%', '80%'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            submitJqueryForm($, index, layero, function (result) {
                                if (result.code === 0) {
                                    layer.close(indexAdd);
                                    layer.msg('保存成功');
                                    table.reload('lay-table');
                                } else {
                                    layer.alert('保存失败');
                                }
                            });
                        }
                    });
                    break;
                case 'delete':
                    layer.msg('已禁用');
                    break;
                case 'update':
                    layer.msg('已禁用');
                    break;
            }
            ;
        });
    });
</script>

<!--模板(操作)-->
<script type="text/html" id="table_op">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view"><i
            class="layui-icon layui-icon-about"></i>查看</a>
    <button sec:authorize="hasAnyRole('ROLE_ADMIN','ROLE_HR')" class="layui-btn layui-btn-normal layui-btn-xs " lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>编辑
    </button>
    <a sec:authorize="hasAnyRole('ROLE_ADMIN')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i
            class="layui-icon layui-icon-delete"></i>删除</a>
    {{# if(d.userType == 2){ }}
    <button class="layui-btn layui-btn-primary layui-btn-xs " lay-event="managerCharge"><i
            class="layui-icon layui-icon-edit"></i>主管
    </button>
    {{# } }}

</script>

<script type="text/html" id="tableHeadToolBarTpl">
    <div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
    <div class="layui-inline layui-hide" lay-event="update"><i class="layui-icon layui-icon-edit"></i></div>
    <div class="layui-inline layui-hide" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>
</script>
</body>
</html>
